package jbp.service.impl;

import jbp.domain.sysuser.AddSysUserDto;
import jbp.domain.sysuser.EditSysUserDto;
import jbp.domain.sysuser.SysUserDetailDto;
import jbp.domain.sysuser.SysUserListDto;
import jbp.entity.SysUserEntity;
import jbp.mapper.SysUserMapper;
import jbp.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    public SysUserDetailDto Query(long id) {
        return sysUserMapper.SelById(id).toDetailDto();
    }

    public int Count(Map<String, Object> params) {
        return sysUserMapper.getCount(params);
    }

    public List<SysUserListDto> Query(Map<String, Object> params) {
        List<SysUserEntity> entityList = sysUserMapper.SelList(params);
        List<SysUserListDto> dtoList = new ArrayList<SysUserListDto>();
        for(int i=0; i<entityList.size(); i++){
            dtoList.add(entityList.get(i).toListDto());
        }
        return dtoList;
    }

    public boolean Add(AddSysUserDto addDto) {
        boolean flag = false;
        try {
            sysUserMapper.Add(addDto.toEntity());
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public boolean Update(EditSysUserDto editDto) {
        boolean flag = false;
        try {
            sysUserMapper.Update(editDto.toEntity());
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public boolean Delete(String id) {
        boolean flag = false;
        try{
            sysUserMapper.Delete(id);
            flag = true;
        } catch(Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
}
